package com.view.resourcemanage.SupplierFrame;

import com.bean.Customer;
import com.bean.Supplier;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
import com.service.CustomerService;
import com.service.SupplierService;
import com.service.impl.CustomerServiceImpl;
import com.service.impl.SupplierServiceImpl;
import com.view.MainFrame;
import com.view.resourcemanage.CustomerFrame.CustomerUpdateDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SupplierUpdateDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private JLabel nameTxt,emailTxt,addressTxt;
    private LovoTxt directorTxt,telTxt,rangeTxt;


    private MainFrame frame ;
    private int key;
    private SupplierService supplierService = new SupplierServiceImpl();

    public SupplierUpdateDialog(MainFrame frame,int key) {
        // TODO Auto-generated constructor stub
        super(frame,"添加新客户",true);
        this.frame = frame;
        this.key = key;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        this.init();
        this.setVisible(true);

    }



    private void init() {
        // TODO Auto-generated method stub
        Supplier supplier = new Supplier();
        supplier = supplierService.getSupplierInfo(key);



        nameTxt = new JLabel();
        nameTxt.setBounds(20,20,100,60);
        nameTxt.setText("<html>客户："+supplier.getName()+" </html>");
        this.add(nameTxt);

        emailTxt = new JLabel();
        emailTxt.setBounds(20,90,100,60);
        emailTxt.setText("<html>经营范围："+supplier.getManageRange()+" </html>");
        this.add(emailTxt);

        addressTxt = new JLabel();
        addressTxt.setBounds(20,160,100,60);
        addressTxt.setText("<html>地址："+supplier.getAddress()+" </html>");
        this.add(addressTxt);


        telTxt = new LovoTxt("联系方式",200,30,this);


        directorTxt = new LovoTxt("负责人",200,110,this);




        this.addBtn = new LovoButton("修改", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SupplierUpdateDialog.this.dispose();;
            }
        });

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String directorTxtText = directorTxt.getText();
                String telTxtText = telTxt.getText();

                if(supplierService.updateSupplier(key,telTxtText,directorTxtText)){
                    JOptionPane.showMessageDialog(null,"修改成功");
                    SupplierUpdateDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"修改失败");
                }
            }
        });

    }

}
